<?php
require_once $_SESSION["app_root"]."config/app.config.php";
class EntityFieldOld
{
	public $ID;
	public $entityID;
	public $fieldType;
	public $name;
	public $dbField;
	public $gridHeader;
	public $gridLocation;
	public $formLocation;
	
	public $entityTable;
	
	/*
	 * 
	 * @return 
	 * @param object $ID
	 */
	public function EntityField($ID){
		if($ID!=0)
		{
			$this->setID($ID);
			$this -> EntityFieldGetByID();
		}
	}
	
	public function CreateEntityFieldBase($entityFieldBaseArray){
		$this->entityID = $entityFieldBaseArray['entityID'];
		$this->name = $entityFieldBaseArray['name'];
		$this->dbField = $entityFieldBaseArray['dbField'];
		if($this->FieldNameIsUnique($this->name, $this->entityID) && $this->DbFieldIsUnique($this->dbField, $this->entityID)){
			$this->ID = uuid();
			$this->fieldType = $entityFieldBaseArray['fieldType'];
			$this->gridHeader = $entityFieldBaseArray['gridHeader'];
			$this->gridLocation = $entityFieldBaseArray['gridLocation'];
			$this->formLocation = $entityFieldBaseArray['formLocation'];
			$createColumnQuery = $this->GenerateCreateColumnQuery($this->dbField, $this->fieldType);
			$db = new Database();
				$db->insertRow("entity_fields", array(
												'id' => $this->ID,
												'entity_id' => $this->entityID,
												'type' => $this->fieldType,
												'name' => $this->name,
												'grid_header' => $this->gridHeader,
												'grid_location' => $this->gridLocation,
												'form_location' => $this->formLocation,
												'db_field' => $this->dbField,
												));
				$db->rawQuery($this->GenerateCreateColumnQuery($this->dbField, $this->fieldType));
			$db->close();
		}
	}
	//ALTER TABLE `entity_fields` CHANGE `id` `id` VARCHAR(36) NOT NULL DEFAULT '' , DROP PRIMARY KEY , ADD PRIMARY KEY ( `id` )
	public function FieldNameIsUnique($_name, $_entityID){
		$QueryString = "SELECT * FROM entity_fields WHERE name = '".strtolower($_name)."' AND entity_id = '".$_entityID."'";
		$db = new Database();
			$result = $db -> rawSelect($QueryString);
		$db -> close();
		return sizeof($result)==1?false:true;
	}
	
	public function DbFieldIsUnique($_dbField, $_entityID){
		$QueryString = "SELECT name FROM entities WHERE id = '".$_entityID."'";
		$db = new Database();
			$result = $db -> rawSelect($QueryString);
			$result = $result[0];
			$this->entityTable = $result['name'];
			$QueryString = "DESCRIBE `".$result['name']."`";
			$cols = $db -> rawSelect($QueryString);
		$db -> close();
		$exists = false;
		foreach($cols as $col){
			if($col['Field'] == $_dbField){
				$exists = true;
				return !$exists; 
			}
		}
		return !$exists;
	}
	
	public function GenerateCreateColumnQuery($columnName, $fieldType){
		$columnType = "VARCHAR(255)";
		switch(strtolower($fieldType)){
			case "textfield": $columnType = "VARCHAR(255)";
				break;
			case "htmleditor":
			case "textarea": $columnType = "TEXT";
				break;
			case "TimeField": $columnType = "VARCHAR(8)";
				break;
			case "datefield": $columnType = "VARCHAR(64)";
				break;
		}
		
		$createColumnQueryString = "ALTER TABLE `".$this->entityTable."` ADD `".$columnName."` ".$columnType." ";
		return $createColumnQueryString;
	}
	
    /**
     * Gets $ID.
     * @see EntityField::$ID
     */
    public function getID()
    {
        return $this->ID;
    }
    
    /**
     * Sets $ID.
     * @param object $ID
     * @see EntityField::$ID
     */
    public function setID($ID)
    {
        $this->ID = $ID;
    }
    
    /**
     * Gets $dbField.
     * @see EntityField::$dbField
     */
    public function getDbField()
    {
        return $this->dbField;
    }
    
    /**
     * Sets $dbField.
     * @param object $dbField
     * @see EntityField::$dbField
     */
    public function setDbField($dbField)
    {
        $this->dbField = $dbField;
    }
    
    /**
     * Gets $entityID.
     * @see EntityField::$entityID
     */
    public function getEntityID()
    {
        return $this->entityID;
    }
    
    /**
     * Sets $entityID.
     * @param object $entityID
     * @see EntityField::$entityID
     */
    public function setEntityID($entityID)
    {
        $this->entityID = $entityID;
    }
    
    /**
     * Gets $formLocation.
     * @see EntityField::$formLocation
     */
    public function getFormLocation()
    {
        return $this->formLocation;
    }
    
    /**
     * Sets $formLocation.
     * @param object $formLocation
     * @see EntityField::$formLocation
     */
    public function setFormLocation($formLocation)
    {
        $this->formLocation = $formLocation;
    }
    
    /**
     * Gets $gridHeader.
     * @see EntityField::$gridHeader
     */
    public function getGridHeader()
    {
        return $this->gridHeader;
    }
    
    /**
     * Sets $gridHeader.
     * @param object $gridHeader
     * @see EntityField::$gridHeader
     */
    public function setGridHeader($gridHeader)
    {
        $this->gridHeader = $gridHeader;
    }
    
    /**
     * Gets $gridLocation.
     * @see EntityField::$gridLocation
     */
    public function getGridLocation()
    {
        return $this->gridLocation;
    }
    
    /**
     * Sets $gridLocation.
     * @param object $gridLocation
     * @see EntityField::$gridLocation
     */
    public function setGridLocation($gridLocation)
    {
        $this->gridLocation = $gridLocation;
    }
    
    /**
     * Gets $name.
     * @see EntityField::$name
     */
    public function getName()
    {
        return $this->name;
    }
    
    /**
     * Sets $name.
     * @param object $name
     * @see EntityField::$name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

}

?>